/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.nodes;

import com.android1.amarena2d.input.EntityTouchSpot;
import com.android1.amarena2d.nodes.behavior.Touchable;
import com.android1.amarena2d.nodes.behavior.delegates.EntityTouchDelegate;
import com.android1.amarena2d.nodes.behavior.delegates.TouchDelegate;

public class EntityGroup extends BaseNode<LeafEntity> implements Touchable<EntityTouchSpot>{

    //todo replace

    protected float left = 0, right = 0, bottom = 0, top = 0;


    public EntityGroup(float x, float y) {
        this.position.x = x;
        this.position.y = y;
    }

    @Override
    public void init() {
        super.init();
        calcSize();

    }

    public void calcSize() {

        final int size = children == null ? 0 : children.size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                final LeafEntity e = children.get(i);
                if (e != null) {
                    final float maxX = e.getAbsoluteMaxX();
                    final float minX = e.getAbsoluteMinX();
                    final float maxY = e.getAbsoluteMaxY();
                    final float minY = e.getAbsoluteMinY();
                    if (maxX > right)
                        right = maxX;
                    if (minX < left)
                        left = minX;
                    if (maxY > top)
                        top = maxY;
                    if (minY < bottom)
                        bottom = minY;
                }
            }
            this.size.width = right - left;
            this.size.height = top - bottom;
        }
    }

    @Override
    public void add(LeafEntity e) {
        super.add(e);
        calcSize();
    }

    @Override
    public void add(LeafEntity... e) {
        super.add(e);
        calcSize();
    }

    @Override
    public void add(LeafEntity e, int zOrder) {
        super.add(e, zOrder);
        calcSize();
    }

    @Override
    public float getAbsoluteMinX() {
        return left;
    }

    @Override
    public float getAbsoluteMinY() {
        return bottom;
    }

    @Override
    public float getAbsoluteMaxY() {
        return top;
    }

    @Override
    public float getAbsoluteMaxX() {
        return right;
    }

    @Override
    public void dispose() {
        super.dispose();
    }


    



@Override
    @SuppressWarnings("unchecked")
    public TouchDelegate<EntityTouchSpot> touch() {
        if (touchDelegate == null)
            touchDelegate = new EntityTouchDelegate(this);
        return touchDelegate;
    }


    public boolean isTouchable() {
        return touchDelegate != null && touchDelegate.isActive();
    }

    @Override
    public boolean onTouchDown(float x, float y, int pointer, boolean isHit) {
        return false;
    }

    @Override
    public boolean onTouchUp(float x, float y, int pointer, boolean isHit) {
        return false;
    }

    @Override
    public boolean onTouchDragged(float x, float y, int pointer, boolean isHit) {
        return false;
    }
}
